package com.application.outdoor.adapter;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;
import com.application.Activity.CommonUtilities;
import com.application.Activity.R;
import com.application.Activity.frmChatPeerActivity;
import com.application.Activity.od_TraLoiVaoNhomActivity;
import com.application.Controller.ReadWriteFileController;
import com.application.Network.Protocol;
import com.application.outdoor.InvoikeServices.HeThongInvoike;
import com.application.outdoor.pojo.TinNhanPOJO;

public class TinNhanAdapter extends BaseAdapter {
	public static class ViewHolder {
		TextView txtTen, txtTinNhan, txtThoiGian;
		Button btnTraLoi;
	}

	ArrayList<TinNhanPOJO> data;
	Context context;
	private LayoutInflater inflater;

	public TinNhanAdapter(Context context, ArrayList<TinNhanPOJO> data) {
		this.context = context;
		this.data = data;
		this.inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	@Override
	public int getCount() {
		return data.size();
	}

	@Override
	public Object getItem(int index) {
		if(data==null||data.size()==0)
			return null;
		return data.get(index);
	}

	@Override
	public long getItemId(int position) {
		if(data==null||data.size()==0)
			return -1;
		return data.get(position).getId();
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		View view = convertView;
		if (view == null) {
			view = inflater.inflate(R.layout.od_row_message, null);
			ViewHolder viewHolder = new ViewHolder();
			viewHolder.txtTen = (TextView) view.findViewById(R.id.txtTen);
			viewHolder.txtTinNhan = (TextView) view.findViewById(R.id.txtTinNhan);
			viewHolder.txtThoiGian=(TextView)view.findViewById(R.id.txtThoiGian);
			viewHolder.btnTraLoi = (Button) view.findViewById(R.id.btnTraLoi);
			view.setTag(viewHolder);
		}

		ViewHolder holder = (ViewHolder) view.getTag();
		holder.txtTen.setText(data.get(position).getNguoiGui()+": ");
		holder.txtTinNhan.setText(data.get(position).getNoiDung());
		holder.txtThoiGian.setText(new SimpleDateFormat("HH:mm:ss dd/MM/yyyy").format(new Date(data.get(position).getThoiGian())) );
		final TinNhanPOJO tinNhan = data.get(position);
		if(tinNhan.getLoai()==1){
			holder.txtTinNhan.setText(" mời bạn vào nhóm");
		}
		holder.btnTraLoi.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				String noiDung = tinNhan.getNoiDung();
				String nguoiGui = tinNhan.getNguoiGui();
				if (tinNhan.getLoai() == 1) {
					
//					String[] m = noiDung.split(";");
//					final int idNhom = Integer.parseInt(m[0]);
//					AlertDialog.Builder builder = new AlertDialog.Builder(context);
//					builder.setTitle("Tin nhắn từ " + data.get(position).getNguoiGui());
//					builder.setMessage(nguoiGui + " mời bạn vào nhóm " + m[1]);
//					builder.setPositiveButton("Đồng ý", new DialogInterface.OnClickListener() {
//						@Override
//						public void onClick(DialogInterface dialog, int which) {
//							try {
//								new HeThongInvoike().dongYVaoNhom(idNhom, tinNhan.getId(),
//										CommonUtilities.nguoiDung.getTenDangNhap());
//							} catch (Exception e) {
//								Log.e(Protocol.TAG,"HeThongInvoike().dongYVaoNhom "+e.getMessage());
//								e.printStackTrace();
//							}
//						}
//					});
//					builder.setNegativeButton("Từ chối", new DialogInterface.OnClickListener() {
//						
//						@Override
//						public void onClick(DialogInterface dialog, int which) {
//							try {
//								new HeThongInvoike().capNhatDaDocTinNhan(tinNhan.getId());
//							} catch (Exception e) {
//								Log.e(Protocol.TAG,"HeThongInvoike().capNhatDaDocTinNhan "+e.getMessage());
//								e.printStackTrace();
//							}
//						}
//					});
//					builder.create().show();
					Intent intent=new Intent(context,od_TraLoiVaoNhomActivity.class);
					intent.putExtra(Protocol.NOI_DUNG, tinNhan.getNoiDung());
					intent.putExtra(Protocol.NGUOI_GUI, tinNhan.getNguoiGui());
					intent.putExtra(Protocol.ID, tinNhan.getId()+"");
					context.startActivity(intent);
					data.remove(position);
				}else {
					final String acc=nguoiGui;
					frmChatPeerActivity._FriendChatting = acc;
					final ArrayList<TinNhanPOJO> lstRemove=new ArrayList<TinNhanPOJO>();
					try{
						for (TinNhanPOJO tn : data) {
							if (tn.getNguoiGui().equals(acc)) {
								ReadWriteFileController.writeTXTPath(ReadWriteFileController.DIRECTORYCHATPEER
										+ "ROLEPeer_" + acc + ".txt", acc + ":" + tn.getNoiDung());
								lstRemove.add(tn);
							}
						}

						Intent intent = new Intent(context, frmChatPeerActivity.class);
						intent.putExtra(frmChatPeerActivity.IP_CHAT_PEER, "");
						intent.putExtra(frmChatPeerActivity.USERNAME, nguoiGui);
						((Activity) context).setResult(frmChatPeerActivity.CODE_CHATPEER, intent);

						context.startActivity(intent);
						Thread t=new Thread(new Runnable() {
							
							@Override
							public void run() {
								try{
									for (TinNhanPOJO tn : lstRemove) {
										if (tn.getNguoiGui().equals(acc)) {
											new HeThongInvoike().capNhatDaDocTinNhan(tn.getId());											
										}
									}
								}catch(Exception e){
									Log.e(Protocol.TAG,"TinNhanAdapter, thread capNhatDaDocTinNhan"+e.getMessage());
								}
								
							}
						});
						t.start();
					}catch (Exception e) {
						Log.e(Protocol.TAG,"TinNhanAdapter click button traloi"+e.getMessage());
					}
					data.removeAll(lstRemove);
				}
				
				notifyDataSetChanged();
			
			}
		});
		return view;

	}

}
